package com.xw.mykotlin

import com.xw.kotlinlib.network.TResp
import com.xw.kotlinlib.KotlinLib
import com.xw.kotlinlib.network.AuthorizationInterceptor
import com.xw.kotlinlib.network.SGsonConverterFactory
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.http.GET
import retrofit2.http.Query
import java.net.Proxy
import java.util.concurrent.TimeUnit

object Api {

    private val mService by lazy {

        val builder = OkHttpClient.Builder()
            .readTimeout(16L,TimeUnit.SECONDS)
            .connectTimeout(16L,TimeUnit.SECONDS)
            .proxy(Proxy.NO_PROXY)
            .addInterceptor(AuthorizationInterceptor())

        if (KotlinLib.isDebug == true) {
            val logInterceptor = HttpLoggingInterceptor()
            logInterceptor.level = HttpLoggingInterceptor.Level.BODY
            builder.addInterceptor(logInterceptor)
        }

        val okHttpClient = builder.build()
        val retrofit = Retrofit.Builder()
            .client(okHttpClient)
            .addConverterFactory(SGsonConverterFactory.create())
            .baseUrl(KotlinLib.BASE_URL)
            .build()

        retrofit.create(ApiService::class.java)
    }

    internal fun getService(): ApiService = mService

    interface ApiService {

        @GET("fcinformation/Plate/v2plateList?status=1")
        suspend fun getCategory(@Query("member_id") member_id: String?,
                                @Query("build_type") build_type: Int?): TResp<List<PCategory>>
    }
}